$(document).ready(function () {
    $(function () {
        $('#collapsed1').collapse('show')
    });

    var widowheight = $(window).height() - 50;
    $("body").css("height", widowheight);

    var pageNum=1;
    var pageSize = 5;
    var total=0;

    $.ajax({
        /*url: "./json/order/list.json!pageNum=1.json",*/
        url: "http://" + urla + "/GetTotalGoodsNumberServlet",
        type: "POST",
        dataType: 'JSON',
        success: function (result) {
            total=result.total;
            console.log(total)
            getPageNum();
            showProductList();
        }
    });
// 时停函数，避免访问过快导致错误
    function sleep(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
                return;
        }
    }
    function getPageNum() {
        /*if (total<=8){
            $("#Previous").on('click',function(){event.preventDefault()}).attr("disabled","disabled");
            $("#Next").on('click',function(){event.preventDefault()}).attr("disabled","disabled");
        }else */
        if(total<=pageSize){
            $("#Previous").on('click',function(){event.preventDefault()}).attr("disabled","disabled");
            $("#Next").on('click',function(){event.preventDefault()}).attr("disabled","disabled");
        }else {
            if (pageNum==1){
                $("#Previous").on('click',function(){event.preventDefault()}).attr("disabled","disabled");
                $("#Next").removeAttr("disabled");
            }else if (pageNum==Math.ceil(total/8)){
                $("#Next").on('click',function(){event.preventDefault()}).attr("disabled","disabled");
                $("#Previous").removeAttr("disabled");
            } else {
                $("#Previous").removeAttr("disabled");
                $("#Next").removeAttr("disabled");
            }
        }
    }

    $("#Previous").click(function () {
        pageNum--;
        $("tbody").empty();
        $.ajax({
            /*url: "./json/order/list.json!pageNum=1.json",*/

            url: "http://" + urla + "/GetGoodsListServlet",
            type: "GET",
            data:{
                "pageSize":pageSize,
                "pageNum":pageNum,
            },
            dataType: 'JSON',
            success: function (result) {
                console.log("GoodList:")
                console.log(result);
                $("tbody").empty();
                $.each(result,function (x,obj) {
                        var $tr=$("<tr>" +
                            "<td>"+obj.good_id+"</td>" +
                            "<td>"+"父类ID："+obj.good_childType+"子类ID："+obj.good_parentType+"</td>"+
                            "<td>"+obj.good_name+"</td>" +
                            "<td>"+obj.good_price+"</td>" +
                            "<td>"+obj.good_remainingAmount+"</td>" +
                            "<td ><a class='btn btn-sm btn-warning' data-toggle='modal' " +
                            "data-target='#change"+obj.good_id+"' id='changeBtn"+obj.good_id+"'>修改信息</a>&nbsp;"+
                            "<a class='btn btn-sm btn-danger' id='pullOffBtn"+obj.good_id+"'>下架</a>" +
                            "<a class='btn btn-sm btn-success' id='pullOnBtn"+obj.good_id+"'>上架</a></td></tr>");
                        $("tbody").append($tr);

                        if (obj.status==1){
                            $("#pullOnBtn"+obj.good_id).remove();
                        }else {
                            $("#pullOffBtn"+obj.good_id).remove();
                        }

                        $("#pullOnBtn"+obj.good_id).click(function () {
                            pullOnProduct(obj.good_id);
                        });
                        $("#pullOffBtn"+obj.good_id).click(function () {
                            pullOffProduct(obj.good_id);
                        });

                        $("#changeBtn"+obj.good_id).click(function () {
                            $(".modal").attr("id","change"+obj.good_id);
                            $(".modal-title").text("修改商品信息,编号"+obj.good_id);
                            changInfo(obj.good_id);
                        });

                })
            }
        });
        getPageNum();
    });

    $("#Next").click(function () {
        pageNum++;
        $("tbody").empty();
        $.ajax({
            /*url: "./json/order/list.json!pageNum=1.json",*/
            url: "http://" + urla + "/GetGoodsListServlet",
            type: "GET",
            data:{
                "pageSize":pageSize,
                "pageNum":pageNum,
            },
            dataType: 'JSON',
            success: function (result) {
                console.log(result);
                $("tbody").empty();
                $.each(result,function (x,obj) {
                        var $tr=$("<tr>" +
                            "<td>"+obj.good_id+"</td>" +
                            "<td>"+"父类ID："+obj.good_childType+"子类ID："+obj.good_parentType+"</td>"+
                            "<td>"+obj.good_name+"</td>" +
                            "<td>"+obj.good_price+"</td>" +
                            "<td>"+obj.good_remainingAmount+"</td>" +
                            "<td ><a class='btn btn-sm btn-warning' data-toggle='modal' " +
                            "data-target='#change"+obj.good_id+"' id='changeBtn"+obj.good_id+"'>修改信息</a>&nbsp;"+
                            "<a class='btn btn-sm btn-danger' id='pullOffBtn"+obj.good_id+"'>下架</a>" +
                            "<a class='btn btn-sm btn-success' id='pullOnBtn"+obj.good_id+"'>上架</a></td></tr>");
                        $("tbody").append($tr);

                        if (obj.status==1){
                            $("#pullOnBtn"+obj.good_id).remove();
                        }else {
                            $("#pullOffBtn"+obj.good_id).remove();
                        }

                        $("#pullOnBtn"+obj.good_id).click(function () {
                            pullOnProduct(obj.good_id);
                        });
                        $("#pullOffBtn"+obj.good_id).click(function () {
                            pullOffProduct(obj.good_id);
                        });

                        $("#changeBtn"+obj.good_id).click(function () {
                            $(".modal").attr("id","change"+obj.good_id);
                            $(".modal-title").text("修改商品信息,编号"+obj.good_id);
                            changInfo(obj.good_id);
                        });

                    })
            }
        });
        getPageNum();

    });

    $("#searchBtn").click(function () {
        $.ajax({
            url:"http://"+urla+"/GetGoodsListServlet",
            type:"GET",
            data:{
                "pageSize":pageSize,
                "pageNum":$("#inputSearch").val(),
            },
            dataType: 'JSON',
            success: function(result){
                console.log(result);
                $("tbody").empty();
                    $.each(result,function (x,obj) {
                        var $tr=$("<tr>" +
                            "<td>"+obj.good_id+"</td>" +
                            "<td>"+"父类ID："+obj.good_childType+"子类ID："+obj.good_parentType+"</td>"+
                            "<td>"+obj.good_name+"</td>" +
                            "<td>"+obj.good_price+"</td>" +
                            "<td>"+obj.good_remainingAmount+"</td>" +
                            "<td ><a class='btn btn-sm btn-warning' data-toggle='modal' " +
                            "data-target='#change"+obj.good_id+"' id='changeBtn"+obj.good_id+"'>修改信息</a>&nbsp;"+
                            "<a class='btn btn-sm btn-danger' id='pullOffBtn"+obj.good_id+"'>下架</a>" +
                            "<a class='btn btn-sm btn-success' id='pullOnBtn"+obj.good_id+"'>上架</a></td></tr>");
                        $("tbody").append($tr);

                        if (obj.status==1){
                            $("#pullOnBtn"+obj.good_id).remove();
                        }else {
                            $("#pullOffBtn"+obj.good_id).remove();
                        }

                        $("#pullOnBtn"+obj.good_id).click(function () {
                            pullOnProduct(obj.good_id);
                        });
                        $("#pullOffBtn"+obj.good_id).click(function () {
                            pullOffProduct(obj.good_id);
                        });

                        $("#changeBtn"+obj.good_id).click(function () {
                            $(".modal").attr("id","change"+obj.good_id);
                            $(".modal-title").text("修改商品信息,编号"+obj.good_id);
                            changInfo(obj.good_id);
                        });

                    })
            },
            error:function (data) {
                console.log("X");
                console.log(data);
            }
        })
    });



    function showProductList() {
        $.ajax({
            url: "http://"+urla+"/GetGoodsListServlet",
            //url: "./json/categories/parent/0/list.json",
            type: "GET",
            data:{
                "pageSize":pageSize,
                "pageNum":pageNum,
            },
            dataType: 'JSON',
            success: function(result){
                console.log(result);
                    $.each(result,function (x,obj) {
                        var $tr=$("<tr>" +
                            "<td>"+obj.good_id+"</td>" +
                            "<td>"+"父类ID："+obj.good_childType+"子类ID："+obj.good_parentType+"</td>"+
                            "<td>"+obj.good_name+"</td>" +
                            "<td>"+obj.good_price+"</td>" +
                            "<td>"+obj.good_remainingAmount+"</td>" +
                            "<td ><a class='btn btn-sm btn-warning' data-toggle='modal' " +
                            "data-target='#change"+obj.good_id+"' id='changeBtn"+obj.good_id+"'>修改信息</a>&nbsp;"+
                            "<a class='btn btn-sm btn-danger' id='pullOffBtn"+obj.good_id+"'>下架</a>" +
                            "<a class='btn btn-sm btn-success' id='pullOnBtn"+obj.good_id+"'>上架</a></td></tr>");
                        $("tbody").append($tr);

                        if (obj.status==1){
                            $("#pullOnBtn"+obj.good_id).remove();
                        }else {
                            $("#pullOffBtn"+obj.good_id).remove();
                        }

                        $("#pullOnBtn"+obj.good_id).click(function () {
                            pullOnProduct(obj.good_id);
                        });
                        $("#pullOffBtn"+obj.good_id).click(function () {
                            pullOffProduct(obj.good_id);
                        });

                        $("#changeBtn"+obj.good_id).click(function () {
                            $(".modal").attr("id","change"+obj.good_id);
                            $(".modal-title").text("修改商品信息,编号"+obj.good_id);
                            changInfo(obj.good_id);
                        });

                    })
            },
            error:function (data) {
                console.log("X");
                console.log(data);
            }
        });
    }

    var successModal=$('#successModal');
    var unknownModal=$('#unknownModal');

    function changInfo(id) {
        console.log(id);
        $.ajax({
            url: "http://" + urla + "/GetGoodInfoServlet",
            type: "GET",
            data:"good_id="+id,
            dataType: 'JSON',
            success: function (result) {
                console.log(result);
                $("#inputName").val(result.good_name);
                $("#inputSubtitle").val(result.subtitle);
                $("#inputPrice").val(result.good_price);
                $("#inputStock").val(result.good_remainingAmount);
                $("#inputDetail").val(result.good_information);

                $("#upload").on("click", function () {
                    $.ajax({
                        type: "POST",
                        cache: true,
                        url: "http://" + urla + "/UploadGoodServlet",
                        data:{
                            "name":$("#inputName").val(),
                            "id":id,
                            "subtitle":$("#inputSubtitle").val(),
                            "price":$("#inputPrice").val(),
                            "stock":$("#inputStock").val(),
                            "parentId":$("#selParentId").val(),
                            "childId":$("#selChlidId").val(),
                            "detail":$("#inputDetail").val(),
                        },
                        error: function (request) {
                            console.log();
                        },
                        success: function (data) {
                            console.log(data);
                            $("#myModal").modal('hide');
                            successModal.modal('show');
                            successModal.css("padding-right","0");
                            successModal.on('hide.bs.modal', function () {
                                location.reload();
                            })
                        }
                    });
                });
            },
            error: function (data) {
                console.log("X");
                console.log(data);
            }
        })
    }

    function pullOffProduct(id) {
        $.ajax({
            type: "POST",
            cache: true,
            url: "http://" + urla + "/PullUpOffGoodServlet",
            data: {
                "id":id,
                "status":0
            },

            error: function (request) {
                console.log();
            },
            success: function (data) {
                console.log(data);
                successModal.modal('show');
                successModal.css("padding-right","0");
                successModal.on('hide.bs.modal', function () {
                    location.reload();
                })
            }
        });
    }

    function pullOnProduct(id) {
        $.ajax({
            type: "POST",
            cache: true,
            url: "http://" + urla + "/PullUpOffGoodServlet",
            data: {
                "id":id,
                "status":1
            },
            error: function (request) {
                console.log();
                console.log(request);
                unknownModal.modal('show');
                unknownModal.css("padding-right","0");
                $("#unknownModal .alert").text(request.msg+"请重试");
                unknownModal.on('hide.bs.modal', function () {
                    location.reload();
                })
            },
            success: function (data) {
                console.log(data);
                successModal.modal('show');
                successModal.css("padding-right","0");
                successModal.on('hide.bs.modal', function () {
                    location.reload();
                })
            }
        });
    }

    $.ajax({
        url: "http://" + urla + "/GetGoodTypeServlet",
        //url: "./json/categories/parent/0/list.json",
        type: "Post",
        dataType: 'JSON',
        success: function (result) {
            console.log(result);
            addChildCategory(result[0].type_id);
                $.each(result, function (x, obj) {
                    var $op = $("<option class='opParent' value='" + obj.type_id + "'>" + obj.type_name + "</option>");
                    $("#selParentId").append($op);
                })
            $('#selParentId').change(function () {
                addChildCategory($(this).val());
                console.log($(this).val());

            });

            /*  $('#selChlidId').change(function () {
             console.log($(this).val());
             });
             */
        },
        error: function (data) {
            console.log("X");
            console.log(data);
        }
    });

    function addChildCategory(id) {
        $.ajax({
            url: "http://" + urla + "/GetChildTypeServlet",
            //url: "./json/categories/parent/0/list.json",
            type: "GET",
            data:"parentId=" + id,
            dataType: 'JSON',
            success: function (result) {
                console.log(result);
                $("#selChlidId").empty();
                    $.each(result, function (x, obj) {
                        var $op2 = $("<option class='opChild' value='" + obj.type_id + "'>" + obj.type_name + "</option>");
                        $("#selChlidId").append($op2);
                    });
            },
            error: function (data) {
                console.log("X");
                console.log(data);
            }
        });
    }


});